import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter_li_ke_jie/entity/app_goods.dart';
import 'package:flutter_li_ke_jie/entity/app_goods_list.dart';
import 'package:flutter_li_ke_jie/entity/app_pay_result.dart';
import 'package:flutter_li_ke_jie/entity/app_product.dart';
import 'package:flutter_li_ke_jie/utils/sputil.dart';

import '../../constant/api_constant.dart';
import '../../core/http/http.dart';
import '../../core/utils/toast.dart';
import '../../utils/log.dart';

class VipModel with ChangeNotifier {

  List<AppGoods> lifeGoods=[];//生活权益商品列表
  List<AppGoods> musicGoods=[];//音乐权益商品列表
  List<AppGoods> videoGoods=[];//视频权益商品列表

  //排名第一的vip产品
  AppProduct firstVipProduct = AppProduct();
//获取权益产品列表
  void getGoodsList() {
    XHttp.get(GOODS_LIST_API).then((response) {
      Log.e("获取权益列表${response}");
      if (response["code"] == 200) {
        //解析获取数据
        lifeGoods = AppGoodsList.fromJson("life",response["data"]).appGoods;
        musicGoods = AppGoodsList.fromJson("music",response["data"]).appGoods;
        videoGoods = AppGoodsList.fromJson("video",response["data"]).appGoods;
        notifyListeners();
      } else {
        ToastUtils.error(response["msg"]);
      }
    });
  }

  //获取权益产品列表
  void getFirstVipProduct() {
    XHttp.get(FIRST_VIP_PRODUCT).then((response) {
      Log.e("获取排名第一的vip产品${response}");
      if (response["code"] == 200) {
        //解析获取数据
        firstVipProduct = AppProduct.fromJson(response["data"]);
        notifyListeners();
      } else {
        ToastUtils.error(response["msg"]);
      }
    });
  }

  //创建订单
  Future<AppPayResult> createVipOrder(num amount,int vipNum) async{
    Map<String,dynamic> map = {};
    map["userId"] = SPUtil.getUserId();
    map["channelId"] = SPUtil.getChannelId();
    map["amount"] = amount;
    map["type"] = 0;
    map["vipNum"] = vipNum;//vip购买月数
    return await XHttp.postJson(CREATE_WAP_PAY,map).then((response){
      Log.e("获取权益列表${response}");
      if (response["code"] == 200) {
        return Future.value(AppPayResult.fromJson(response["data"]));
      }else{
        ToastUtils.error(response["msg"]);
        return Future.value(null);
      }
    });
  }

  //查询支付状态
  Future<AppPayResult> queryPayResult(String outTradeNo,String shopdate,int vipNum) async{
    Map<String,dynamic> map = {};
    map["outTradeNo"] = outTradeNo;
    map["shopdate"] = shopdate;
    map["userId"] = SPUtil.getUserId();//用户id需要
    map["payMonthNum"] = vipNum;//购买的月数
    return await XHttp.postJson(QUERY_WAP_PAY_RESULT,map).then((response){
      Log.e("获取权益列表${response}");
      if (response["code"] == 200) {
        return Future.value(AppPayResult.fromJson(response["data"]));
      }else{
        ToastUtils.error(response["msg"]);
        return Future.value(null);
      }
    });
  }
}